package ex2.employee;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CSpecialist extends CEmployee implements IEmployee {

	private IEquipmentHandler _EquipmentHandler;
	private Collection<String> _equipment;
	
	public CSpecialist(String record,IEmployeeHandler employeeHandler ,IEquipmentHandler equipmentHandler){
		super(record, employeeHandler);
		
		this._equipment = new ArrayList<String>();
		
		if(this._extraData != null){
			String tExtraData = this._extraData.trim();
			String tRecordToAdd = this._parser(tExtraData);
		
			while (!tRecordToAdd.isEmpty())
			{
				this._equipment.add(tRecordToAdd);
						
				if (tRecordToAdd.length()+1  > tExtraData.length()) break;
			
				tExtraData = (tExtraData.substring(tRecordToAdd.length()+1)).trim();
				tRecordToAdd = this._parser(tExtraData);
			}
		}
		
		this._EquipmentHandler = equipmentHandler;
		
	}
	
	@Override
	public String getFirstName() {
		return super.getFirstName();
	}

	@Override
	public String getLastName() {
		return super.getLastName();
	}

	@Override
	public String getUsername() {
		return super.getUsername();
	}

	@Override
	public double getSalary() {
		return super.getSalary();
	}

	@Override
	public double calaulateTotalExpenses() {
		
		double tAnswer = getSalary();
		
		for(Iterator<String> iterator = this._equipment.iterator(); iterator.hasNext();)
			
			tAnswer += this._EquipmentHandler.getCost(iterator.next());
		
		return tAnswer;
	}
	
	
	public String getEquipment(){
		String tAnswer= "";
		
		for(Iterator<String> iterator = this._equipment.iterator(); iterator.hasNext();){
			
			tAnswer += iterator.next();
			
			if(iterator.hasNext())
				tAnswer = tAnswer.concat(", ");
		}
		return tAnswer;
	}
	
	
	
	public void clearRedundantEmployees() {

		Collection<String> tEquipmentToRemove = new ArrayList<String>();
		for(Iterator<String> iterator = this._equipment.iterator(); iterator.hasNext();){
			
			String tEquipmentName = iterator.next();
			
			if(!this._EquipmentHandler.isAvailable(tEquipmentName))
				tEquipmentToRemove.add(tEquipmentName);
			
		}
		
		for(Iterator<String> iterator = tEquipmentToRemove.iterator(); iterator.hasNext();)	
			this._equipment.remove(iterator.next());
	}
}
